package com.tian.recycler.holder

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2021/3/22 10:12
 */
class SimpleDividerDecoration(paintColor: Int) : RecyclerView.ItemDecoration() {


    private val dividerPaint: Paint = Paint().apply {
        color = paintColor
        //style = Paint.Style.STROKE
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.bottom = 2
    }


    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        for (index in 0 until parent.childCount) {
            val view = parent.getChildAt(index)
            c.drawRect(parent.paddingLeft.toFloat(), view.bottom.toFloat(),
                    (parent.width - parent.paddingRight).toFloat(), (view.bottom + 2).toFloat(), dividerPaint)
        }
    }
}